package com.foreveross.crawl.workers;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.UnableToInterruptJobException;

import com.foreveross.crawl.common.cfg.PropertyUtils;
import com.foreveross.crawl.common.cfg.system.SystemPropertiesLoader;
import com.foreveross.crawl.common.exception.BusinessException;
import com.foreveross.util.FileUtil;
import com.foreveross.util.GlobalVariable;
import com.foreveross.util.PropertesCustom;

/** 
 * @ClassName: FilePollWorker 
 * @Description: 定时扫描指定目录，并将其进行上传；处理异常的压缩文件
 * @author luofangyi
 * @date 2014-8-7 下午2:34:34 
 *  
 */ 
public class FilePollWorker extends AbstractTaskWorker {

	public static Logger logger = Logger.getLogger(FilePollWorker.class);

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {

		logger.info("定时轮询任务开始");
		try {
			parseFileUpload();
			deleteUploadFailByZip();
			uploadFailByZip();
		} catch (Exception e) {
			logger.error(e);
		}
		logger.info("定时轮询任务结束");
	}
	
	/**  
	 * @Description: 上传解析文件
	 * @author luofangyi
	 * @throws BusinessException 
	 * @date 2014-8-7 下午6:33:02 
	 */ 
	private void parseFileUpload(){
		try {
			double intervalTime = Double.parseDouble(PropertyUtils.getProperty(PropertesCustom.SYSTEM_INTERVAL_TIME, SystemPropertiesLoader.FILE_NAME, "1.5"));
			String path = FileUtil.getFilePath(FileUtil.getParseFilePath() + "\\" + GlobalVariable.parse_filename);
			if(FileUtil.isOperatorByTimeout(path, intervalTime)){
				logger.info("准备执行定时上传:" + path);
				String persistenceUrl = PropertyUtils.getProperty(PropertesCustom.SYSTEM_DATAPERSISTENCE_DOWNLOAD_URL, SystemPropertiesLoader.FILE_NAME, "");
				//重试上传次数
				int reTry = Integer.parseInt(PropertyUtils.getProperty(PropertesCustom.SYSTEM_DATAPERSISTENCE_DOWNLOAD_RETRY_COUNT, SystemPropertiesLoader.FILE_NAME, "3"));
				this.iTaskOperateApplication.uploadFile(persistenceUrl, reTry);
			}
		} catch (BusinessException e) {
			logger.error(e.getMessage(), e);
		}
	}

	
	
	/**  
	 * @Description: 删除超过指定时间的压缩文件
	 * @author luofangyi
	 * @date 2014-8-7 下午6:35:50 
	 */ 
	private void deleteUploadFailByZip() throws Exception{
		Map<Boolean, Set<String>> operatorMap = decideZipIsTimeout();
		Set<String> pathSet = operatorMap.get(true);
		
		logger.info("准备删除超时压缩文件");
		for(String path : pathSet){
			FileUtil.deleteForlder(path);
			logger.debug(path);
		}
	}

	/**  
	 * @Description: 判断zip文件是否超时
	 * @return
	 * @author luofangyi
	 * @date 2014-8-10 上午9:42:25 
	 */ 
	private Map<Boolean, Set<String>> decideZipIsTimeout() throws Exception {
		Map<Boolean, Set<String>> retMap = new HashMap<Boolean, Set<String>>();
		double intervalTime = Double.parseDouble(PropertyUtils.getProperty(PropertesCustom.SYSTEM_INTERVAL_ZIP_TIME, SystemPropertiesLoader.FILE_NAME, "24"));
		String pathDirectory = FileUtil.zipPath;
		File fileDirectory = new File(pathDirectory);
		File[] files = fileDirectory.listFiles();
		Set<String> delPath = new HashSet<String>();
		Set<String> uploadPath = new HashSet<String>();
		retMap.put(true, delPath);
		retMap.put(false, uploadPath);
		for(File file : files){//获取目录下所有文件信息
			String path = file.getAbsolutePath();
			if(path.contains(FileUtil.zipSuffix)){//文件路径是否包含.zip字符
				try {
					if(FileUtil.isOperatorByTimeout(path, intervalTime)){//超时需删除文件
						retMap.get(true).add(path);
					} else if(path.contains(FileUtil.exceptionSuffix)) {//未超时，但上传发生异常的文件
						retMap.get(false).add(path);
					}
				} catch (BusinessException e) {
					logger.error(e.getMessage(), e);
				}
				
			}
		}
		return retMap;
	}
	
	/**  
	 * @Description: 将上传失败的zip文件重新上传
	 * @author luofangyi
	 * @throws Exception 
	 * @date 2014-8-7 下午6:34:29 
	 */ 
	private void uploadFailByZip() throws Exception{
		Map<Boolean, Set<String>> operatorMap = decideZipIsTimeout();
		Set<String> pathSet = operatorMap.get(false);
		
		int reTry = Integer.parseInt(PropertyUtils.getProperty(PropertesCustom.SYSTEM_DATAPERSISTENCE_DOWNLOAD_RETRY_COUNT, SystemPropertiesLoader.FILE_NAME, "3"));
		
		logger.info("准备上传压缩文件");
		for(String path : pathSet){
			logger.debug(path);
			//重试上传次数
			this.iTaskOperateApplication.uploadFile(path, reTry);
		}
	}

	@Override
	public void interrupt() throws UnableToInterruptJobException {
		// TODO Auto-generated method stub
		
	}
	
	public static void main(String[] args) throws Exception{
		new FilePollWorker().parseFileUpload();
		new FilePollWorker().deleteUploadFailByZip();
		new FilePollWorker().uploadFailByZip();
	}
}
